package com.woniuxy.dr_mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.dr_mall.entity.DrPicture;
import com.woniuxy.dr_mall.entity.DrSpec;
import com.woniuxy.dr_mall.entity.VDrProduct;
import com.woniuxy.dr_mall.service.IDrCategoryService;
import com.woniuxy.dr_mall.service.IDrPictureService;
import com.woniuxy.dr_mall.service.IDrSpecService;
import com.woniuxy.dr_mall.service.IDrVProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 张哲轩
 * @version 1.0.0
 * @ClassName IndexController.java
 * @createTime 2021年08月12日 19:15:00
 */
@Controller
@Slf4j
@RequestMapping("index")
public class IndexController {
    @Autowired
    private IDrSpecService specService;

    @Autowired
    private IDrCategoryService categoryService;
    @Autowired
    private IDrVProductService vProductService;

    @Autowired
    private IDrPictureService pictureService;

    @GetMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        log.debug("id:{}",id);
        List<DrSpec> specs = specService.getNotNullSpecs(id);
        QueryWrapper<DrPicture> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("productId",id);
        queryWrapper.eq("isdefault","y");
        model.addAttribute("specs", specs);
        model.addAttribute("productId", id);
        model.addAttribute("picture",pictureService.getOne(queryWrapper));
        log.debug("p:{}",pictureService.getOne(queryWrapper));
        return "detail";
    }

    @GetMapping("lists")
    public String lists(Model model, VDrProduct product, Integer current) {
        log.debug("current:{}",current);
        log.debug("product:{}",product);
        if (current==null){
            current=1;
        }
        //封装了分页查询的相关的属性
        Page<VDrProduct> page = new Page<>(current, 4);
        //封装了查询条件
        QueryWrapper<VDrProduct> queryWrapper = new QueryWrapper<>();
        //判断前台是否模糊查询名字
        if (product.getName() != null && product.getName().trim().length() != 0) {
            queryWrapper.like("name", product.getName());
        }
        //判断是否传了分类
        if (product.getCategoryId()!=null&&product.getCategoryId()!=0){
            queryWrapper.eq("categoryid",product.getCategoryId());
        }
        //判读是否查询爆款热卖
        if (product.getHot() != null) {
            queryWrapper.eq("hot", "y");
        }
        //判读是否查询女生最爱
        if (product.getFavor() != null) {
            queryWrapper.eq("favor", "y");
        }
        //判读是否查询人气最高
        if (product.getPopular() != null) {
            queryWrapper.eq("popular", "y");
        }
        //判断前台是否传了最低值
        if (product.getMinPrice() != null && product.getMinPrice().trim().length() != 0) {
            queryWrapper.ge("price", product.getMinPrice());
        }
        //判断前台是否传了最大值
        if (product.getMaxPrice() != null && product.getMaxPrice().trim().length() != 0) {
            queryWrapper.le("price", product.getMaxPrice());
        }
        Page<VDrProduct> productPage = vProductService.page(page, queryWrapper);
        log.debug("productPage:{}",productPage);
        //
        int totalPage = (int) (productPage.getTotal() % productPage.getSize()==0?productPage.getTotal() / productPage.getSize():productPage.getTotal() / productPage.getSize()+1);
        //定义要遍历的页码数组
        int[] pages=new int[totalPage];
        for (int i=1;i<=totalPage;i++){
            pages[i-1]=i;
        }
        model.addAttribute("pages",pages);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("page",productPage);
        //前台传过来的条件
        model.addAttribute("vproduct", product);
        //当前页码
        model.addAttribute("currentPage", current);
        model.addAttribute("categories", categoryService.getAll());
        //前台回显数据
        model.addAttribute("categoryid", product.getCategoryId());
        log.debug("pages:{}",pages);
        return "lists";
    }

    @GetMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/";

    }
}
